package net.zdreamer.geracomium.adapter;

import java.util.ArrayList;

import net.zdreamer.geracomium.utils.PersonalinfoUtils.LeafDao;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.geracomium.R;

public class TreeLeafAdapter extends BaseAdapter {

	private ArrayList<LeafDao> elementsData;

	public void setElementsData(ArrayList<LeafDao> elementsData) {
		this.elementsData = elementsData;
	}

	private ArrayList<LeafDao> elements;

	public void setElements(ArrayList<LeafDao> elements) {
		this.elements = elements;
	}

	/** LayoutInflater */
	private LayoutInflater inflater;
	private int indentionBase;
	private Context context;

	public TreeLeafAdapter(ArrayList<LeafDao> elements,
			ArrayList<LeafDao> elementsData, LayoutInflater inflater,
			Context context) {
		this.elements = elements;
		this.elementsData = elementsData;
		this.inflater = inflater;
		this.context = context;
		indentionBase = 20;
	}

	public ArrayList<LeafDao> getElements() {
		return elements;
	}

	public ArrayList<LeafDao> getElementsData() {
		return elementsData;
	}

	@Override
	public int getCount() {
		return elements.size();
	}

	@Override
	public Object getItem(int position) {
		return elements.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.treeleaf_item, null);
			holder.contentName = (TextView) convertView
					.findViewById(R.id.contentName);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		LeafDao element = elements.get(position);
		int level = element.getLevel();
		if (level == 1) {
			RelativeLayout rela = (RelativeLayout) convertView
					.findViewById(R.id.tree);
			rela.setBackgroundResource(R.drawable.childtreeback);
		} else if (level == 0) {
			RelativeLayout rela = (RelativeLayout) convertView
					.findViewById(R.id.tree);
			rela.setBackgroundResource(R.drawable.treeback);
		} else if (level == 2) {
			RelativeLayout rela = (RelativeLayout) convertView
					.findViewById(R.id.tree);
			rela.setBackgroundResource(R.drawable.background);
		}

		holder.contentName.setText(element.getContentText().getName());
		// holder.contentText.setText(element.getContentText().getText());

		return convertView;
	}

	static class ViewHolder {
		TextView contentName;
	}

}
